<?php

class PageController extends AppController {
	
	public function actionView($pageName = 'home') {
		if (strpos($pageName, '../') !== false) {
			throw new Lvc_Exception('File Not Found: ' . $sourceFile);
		}
		
		$this->setLayoutVar('pageTitle', 'Flextrine Manager');
		
		$this->requireJs("jquery.min.js");
		$this->requireJs("page.js");
		
		// Get the FURL (Flextrine URL)
		$furl = isset($this->get["furl"]) ? $this->get["furl"] : "";
		
		// Get the action
		$action = isset($this->get["action"]) ? $this->get["action"] : null;
		
		if ($action) {
			switch ($action) {
				case "generate":
					$tempFilename = $this->generatedClassesToZip(json_decode(@file_get_contents($furl."/manager.php?action=generate")));
					break;
				case "create":
					$result = json_decode(@file_get_contents($furl."/manager.php?action=create"));
					break;
				case "update":
					$result = json_decode(@file_get_contents($furl."/manager.php?action=update"));
					break;
				case "drop":
					$result = json_decode(@file_get_contents($furl."/manager.php?action=drop"));
					break;
				default:
					break;
			}
			
			if (isset($result->success)) $this->setVar("successFlash", $result->success);
			if (isset($result->failure)) $this->setVar("errorFlash", $result->failure);
		}
		
		if ($furl) {
			$serviceDetails = json_decode(@file_get_contents($furl."/manager.php"));
			
			if ($serviceDetails) {
				// Get the database name from the service details
				$this->setVar("dbname", $serviceDetails->dbname);
				
				// Construct a <li> list of entities from the service details
				$entitiesList = "";
				if ($serviceDetails->entities) {
					foreach ($serviceDetails->entities as $entity)
						$entitiesList .= "<li>$entity</li>";
					
					$this->setVar("entitiesList", $entitiesList);
				}
			} else {
				// If $serviceDetails is null we couldn't read manager.php
				$this->setVar("errorFlash", "Unable to access ".$furl."/manager.php - check manager.php exists and flextrineManagerEnabled is true in services/FlextrineService.php");
			}
		}
		
		$this->setVar("furl", $furl);
		
		if ($action != "generate") {
			$this->loadView('page/' . rtrim($pageName, '/'));
		} else {
			if ($furl) {
				header('Content-type: application/zip');
				header('Content-Disposition: attachment; filename="FlextrineAS3Classes.zip"');
				
				echo($tempFilename);
				
				$this->setLayout(null);
				$this->loadView('page/home');
			}
		}
	}
	
	private function generatedClassesToZip($generatedClasses) {
		/*$tempFilename = tempnam(sys_get_temp_dir(), 'Flextrine');
		
		$zip = new ZipArchive();
		$zip->open($tempFilename, ZIPARCHIVE::CREATE);
		
		foreach ($generatedClasses as $path => $generatedClass)
			$zip->addFromString($path, $generatedClass);
		
		return $tempFilename;*/
		include dirname(__FILE__)."/../classes/zip_min.inc";
		$zipfile = new zipfile();
		
		//$tempFilename = tempnam(sys_get_temp_dir(), 'Flextrine');
		
		foreach ($generatedClasses as $path => $generatedClass)
			$zipfile->addFile($generatedClass, $path);
		
		return $zipfile->file();
	}
	
}

?>